package com.ruicar.afs.cloud.batch.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.batch.entity.AfsBatchJobGroupInfo;
import com.ruicar.afs.cloud.batch.entity.AfsBatchLogGroup;
import com.ruicar.afs.cloud.batch.enums.MessageType;
import com.ruicar.afs.cloud.batch.enums.RunStatus;
import com.ruicar.afs.cloud.batch.frame.logger.WsMonitorHelper;
import com.ruicar.afs.cloud.batch.mapper.AfsBatchLogGroupMapper;
import com.ruicar.afs.cloud.batch.service.AfsBatchInfoService;
import com.ruicar.afs.cloud.batch.service.AfsBatchLogGroupService;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.data.datasource.annotation.DS;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;

@Service
@Slf4j
@AllArgsConstructor
public class AfsBatchLogGroupServiceImpl extends ServiceImpl<AfsBatchLogGroupMapper, AfsBatchLogGroup> implements AfsBatchLogGroupService {
    private final AfsBatchInfoService afsBatchInfoService;
    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public RunStatus doLog(AfsBatchJobGroupInfo afsBatchJobGroupInfo, String bhDate) {
        AfsBatchLogGroup afsBatchLogGroup = this.getOne(Wrappers.<AfsBatchLogGroup>lambdaQuery().eq(AfsBatchLogGroup::getBatchNo, afsBatchJobGroupInfo.getBatchNo()).eq(AfsBatchLogGroup::getSystemName, afsBatchJobGroupInfo.getSystemName()).eq(AfsBatchLogGroup::getBatchDate,bhDate));
        if(afsBatchLogGroup ==null) {
            afsBatchLogGroup = new AfsBatchLogGroup();
            afsBatchLogGroup.setBatchDate(bhDate);
            afsBatchLogGroup.setBatchStatus(RunStatus.RUNNING);
            afsBatchLogGroup.setBatchNo(afsBatchJobGroupInfo.getBatchNo());
            afsBatchLogGroup.setSystemName(afsBatchJobGroupInfo.getSystemName());
            afsBatchLogGroup.setStartTime(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN));
            this.save(afsBatchLogGroup);
        }else{
            if(afsBatchLogGroup.getBatchStatus()==RunStatus.ERROR){
                log.info("任务[{}]-[{}]-[{}]处于异常状态，恢复至正常状态继续进行", afsBatchJobGroupInfo.getSystemName(), afsBatchJobGroupInfo.getBatchNo(), afsBatchJobGroupInfo.getBatchName());
                WsMonitorHelper.sendMessage(MessageType.INFO, MessageFormat.format("任务[{0}]-[{1}]-[{2}]处于异常状态，恢复至正常状态继续进行", afsBatchJobGroupInfo.getSystemName(), afsBatchJobGroupInfo.getBatchNo(), afsBatchJobGroupInfo.getBatchName()));
                afsBatchLogGroup.setBatchStatus(RunStatus.RUNNING);
            }
            this.updateById(afsBatchLogGroup);
        }
        return afsBatchLogGroup.getBatchStatus();
    }

    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void saveError(AfsBatchJobGroupInfo afsBatchJobGroupInfo, String bhDate) {
        AfsBatchLogGroup afsBatchLogGroup = this.getOne(Wrappers.<AfsBatchLogGroup>lambdaQuery().eq(AfsBatchLogGroup::getBatchNo, afsBatchJobGroupInfo.getBatchNo()).eq(AfsBatchLogGroup::getSystemName, afsBatchJobGroupInfo.getSystemName()).eq(AfsBatchLogGroup::getBatchDate,bhDate));
        if(afsBatchLogGroup ==null) {
            afsBatchLogGroup = new AfsBatchLogGroup();
            afsBatchLogGroup.setBatchDate(bhDate);
            afsBatchLogGroup.setBatchStatus(RunStatus.ERROR);
            afsBatchLogGroup.setBatchNo(afsBatchJobGroupInfo.getBatchNo());
            afsBatchLogGroup.setSystemName(afsBatchJobGroupInfo.getSystemName());
            afsBatchLogGroup.setStartTime(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN));
            this.save(afsBatchLogGroup);
        }else{
            afsBatchLogGroup.setBatchStatus(RunStatus.ERROR);
            this.updateById(afsBatchLogGroup);
        }
    }

    @Override
    @DS(CommonConstants._BASE_DS_NAME)
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void finished(AfsBatchJobGroupInfo afsBatchJobGroupInfo, String bhDate) {
        AfsBatchLogGroup afsBatchLogGroup = this.getOne(Wrappers.<AfsBatchLogGroup>lambdaQuery().eq(AfsBatchLogGroup::getBatchNo, afsBatchJobGroupInfo.getBatchNo()).eq(AfsBatchLogGroup::getSystemName, afsBatchJobGroupInfo.getSystemName()).eq(AfsBatchLogGroup::getBatchDate,bhDate));
        if(afsBatchLogGroup ==null) {
            afsBatchLogGroup = new AfsBatchLogGroup();
            afsBatchLogGroup.setBatchDate(bhDate);
            afsBatchLogGroup.setBatchStatus(RunStatus.FINISHED);
            afsBatchLogGroup.setBatchNo(afsBatchJobGroupInfo.getBatchNo());
            afsBatchLogGroup.setSystemName(afsBatchJobGroupInfo.getSystemName());
            afsBatchLogGroup.setStartTime(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN));
            afsBatchLogGroup.setFinishTime(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN));
            this.save(afsBatchLogGroup);
        }else{
            afsBatchLogGroup.setBatchStatus(RunStatus.FINISHED);
            afsBatchLogGroup.setFinishTime(DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_PATTERN));
            this.updateById(afsBatchLogGroup);
        }
    }
}
